package com.demo.util;

import com.demo.entiy.GoodJd;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author：不想早起
 * @Package：com.demo.util
 * @Project：ESDemo
 * @name：PyJDUtils
 * @Date：2024/2/27 21:03
 * @Filename：PyJDUtils
 */
public class PyJDUtils {
    @Autowired
    private RestHighLevelClient client;

    public ArrayList<GoodJd> PyJDUtils(String keyWords) throws IOException {
        String url ="https://search.jd.com/Search?keyword="+keyWords;
        Document document = Jsoup.parse(new URL(url), 900000000);
        Element element = document.getElementById("J_goodsList");
        Elements elements = element.getElementsByTag("li");
        ArrayList<GoodJd> goodJDlist = new ArrayList<>();
        for (Element el : elements) {
            String img = el.getElementsByTag("img").eq(0).attr("data-lazy-img");
            System.out.println("img======>"+img);

            String price = el.getElementsByClass("p-price").eq(0).text();
            System.out.println("price=======>"+price);

            String title = el.getElementsByClass("p-name").eq(0).text();
            System.out.println("title======>"+title);

            String shopnum = el.getElementsByClass("p-shopnum").eq(0).text();
            System.out.println("shopnum=======>"+shopnum);

            GoodJd goodJD = new GoodJd();
            goodJD.setTitle(title);
            goodJD.setImg(img);
            goodJD.setPrice(price);
            goodJD.setShopnum(shopnum);
            goodJDlist.add(goodJD);
        }
        return goodJDlist;
    }

    public static void main(String[] args) throws Exception {

        String url ="https://search.jd.com/Search?keyword=java";
        // 设置cookie
        Map<String, String> cookies = new HashMap<String, String>();
        cookies.put("thor", "5ACBD18EBA0001023B298A6573EF17E4431423EFF738F25218C25D53E03ADCEF20470E795953A401ED36896A838F360224AF89F288F7B068FAE44F302AD30E314B645ECEC5CEA2DA1D14A395D15DAD6799FFE50FE3F42FC1AA6AA82F5FB23C482459517301CA7B8297A971C944BBA1E563B2B2CD7B5E6221EA351105FCEB2BE82A4F96E48073F8E314679EA29E076E63904FEB823E6593CB15B843029A1F615E");
        //  2.解析网页 返回的document对象就是浏览器的Document对象
        Document document = Jsoup.connect(url).cookies(cookies).get();

        Element element = document.getElementById("J_goodsList");
        Elements elements = element.getElementsByTag("li");
        ArrayList<GoodJd> goodJDlist = new ArrayList<>();
        for (Element el : elements) {
            String img = el.getElementsByTag("img").eq(0).attr("data-lazy-img");
            System.out.println("img======>"+img);

            String price = el.getElementsByClass("p-price").eq(0).text();
            System.out.println("price=======>"+price);

            String title = el.getElementsByClass("p-name").eq(0).text();
            System.out.println("title======>"+title);

            String shopnum = el.getElementsByClass("p-shopnum").eq(0).text();
            System.out.println("shopnum=======>"+shopnum);

            GoodJd goodJD = new GoodJd();
            goodJD.setTitle(title);
            goodJD.setImg(img);
            goodJD.setPrice(price);
            goodJD.setShopnum(shopnum);
            goodJDlist.add(goodJD);


        }
    }
}
